package com.aixin.csopsupplier.controller;

import com.aixin.common.constant.Constants;
import com.aixin.common.core.domain.AjaxResult;
import com.aixin.common.core.domain.model.LoginBody;
import com.aixin.common.exception.user.UserPasswordNotMatchException;
import com.aixin.csopsupplier.model.SupplierDefModel;
import com.aixin.csopsupplier.model.SystemReturn;
import com.aixin.csopsupplier.service.SupplierDefService;
import com.aixin.csopsupplier.utils.Util;
import com.aixin.csopsupplier.domain.SupplierDef;
import com.aixin.framework.web.service.SysLoginService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;


@RestController
@RequestMapping("/supplier")
@EnableAutoConfiguration
public class SupplierDefController {

    @Autowired
    private SysLoginService loginService;

    @Autowired
    SupplierDefService supplierDefService;

    @RequestMapping("/getPageSupplier")
    public PageInfo<SupplierDef> getPageSupple(SupplierDefModel supplierDefModel, int pageNum, int pageSize) {
        return supplierDefService.findAll(supplierDefModel, pageNum, pageSize);
    }

    @RequestMapping("/getSupplier")
    public List<SupplierDef> getSupplier() {
        return supplierDefService.findAll();
    }

    @RequestMapping("/saveSupplier")
    public SystemReturn saveSupplier(@RequestBody SupplierDefModel SupplierDefModel) {
        if (supplierDefService.getSupplierByUserName(SupplierDefModel.getUserName())) {
            return supplierDefService.save(SupplierDefModel);
        }
        SystemReturn systemReturn = SystemReturn.getSystemFalseReturn();
        systemReturn.setContent("用户名已经存在");
        return systemReturn;
    }

    @RequestMapping("/updateSupplier")
    public SystemReturn updateSupplier(@RequestBody SupplierDefModel supplierDefModel) {
        return supplierDefService.update(supplierDefModel);
    }

    @RequestMapping("/login")
    public AjaxResult supplierLogin(@RequestBody LoginBody loginBody) {
        AjaxResult ajax = AjaxResult.success();
        // 生成令牌
        SupplierDef supplierDef = supplierDefService.loginSupplier(loginBody);
//        if (supplierDef == null) {
//            throw new UserPasswordNotMatchException();
//        }

        String sysAccount = "supplier";
        String sysPwd = "supplier0419";
        if (supplierDef == null) {
            // 供应商登录失败，作为后台用户登录
            sysAccount = loginBody.getUsername();
            sysPwd = loginBody.getPassword();
        } else {
            ajax.put("supplier", supplierDef);
        }

        String token = loginService.login(sysAccount, sysPwd, loginBody.getCode(),
                loginBody.getUuid());
//        if (supplierDef == null && "".equals(supplierDef.getSupplierID())) {
//            token = null;
//        }
        ajax.put(Constants.TOKEN, token);
        return ajax;
    }

    @RequestMapping("/updatePassword")
    public SystemReturn updatePassword(@RequestBody SupplierDefModel supplierDefModel) {
        return supplierDefService.updatePassword(supplierDefModel);
    }

}
